package com.javakademi.tag.elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bora SAYINER
 * @since Jan 14, 2015 11:50:27 PM
 * @version 1.0
 */
public class TextFieldTag extends SimpleTagSupport implements DynamicAttributes {

	private String text;
	private String hint;
	private boolean password;

	private final Map<String, Object> attributes;

	public TextFieldTag() {
		this.attributes = new HashMap<String, Object>();
		setText("");
		setHint("");
	}

	@Override
	public void doTag() throws JspException, IOException {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(String.format("<input type='%s' value='%s' placeholder='%s' ", password ? "password" : "text", text, hint));
		Set<Entry<String, Object>> entrySet = attributes.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			stringBuilder.append(String.format("%s='%s' ", entry.getKey(), entry.getValue()));
		}
		stringBuilder.append("/>");
		getJspContext().getOut().println(stringBuilder);
	}

	@Override
	public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
		this.attributes.put(localName, value);
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setHint(String hint) {
		this.hint = hint;
	}

	public void setPassword(boolean password) {
		this.password = password;
	}
}
